//
//  MineMenuTableViewCell.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/3/13.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "MineMenuTableViewCell.h"
#import "UIButton+ImageTitleSpacing.h"

@interface MineMenuTableViewCell ()

@property (nonatomic,strong)NSArray *mineMenuArray;

@end

@implementation MineMenuTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self loadMineMenuListView];
    }
    return self;
}

- (void)loadMineMenuListView {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,WIDTH,HEIGHT/4)];
    //    view.backgroundColor = RandColor;
    [self.contentView addSubview:view];
    
    for (int i = 0; i < 7; ++i) {
        
        CGFloat HIGH = HEIGHT/8;
        
        int maxCol = 4;
        CGFloat col = i % maxCol;
        CGFloat row = i / maxCol;
        
        CGRect frame = CGRectMake(col*WIDTH/4,10+row*HIGH,WIDTH/4, HIGH);
        
        UIView *menubackView = [[UIView alloc] initWithFrame:frame];
        menubackView.tag = 10+i;
        [self addSubview:menubackView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickMineMenuViewList:)];
        
        [menubackView addGestureRecognizer:tap];
    
        
        CGFloat frameWidth = frame.size.width/1.5;
        //图
        CGFloat margin = (WIDTH/4 - frameWidth)/2;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(margin,5, frameWidth, frameWidth/2+5)];
        //        imageView.layer.cornerRadius = frameWidth/2;
        //        imageView.layer.masksToBounds = YES;
        //        imageView.contentMode = UIViewContentModeScaleToFill;
        //        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"wode_0%d",i + 1]];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"wode_0%d",i+1]];
        imageView.contentMode = UIViewContentModeCenter;
        [menubackView addSubview:imageView];
        
        //文字
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame)+5,WIDTH/4, 20)];
        titleLabel.font = [UIFont systemFontOfSize:12];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = [NSString stringWithFormat:@"%@",self.mineMenuArray[i]];
        titleLabel.textColor = [UIColor grayColor];
        [menubackView addSubview:titleLabel];
        
//        if (i == 7) {
//            self.lastLabel = titleLabel;
//            self.lastImageView = imageView;
//        }
        
        CGFloat x = 0.0;
        CGFloat y = 0.0;
        CGFloat w = 0.0;
        CGFloat h = 0.0;
        CGFloat width = WIDTH/4;
        CGFloat height = HEIGHT/4;
        switch (i) {
            case 0:
                x = width-1;
                y = 3;
                w = 1;
                h = height - 6;
                break;
                
            case 1:
                x = width * 2;
                y = 3;
                w = 1;
                h = height - 6;
                break;
                
            case 2:
                x = width * 3;
                y = 3;
                w = 1;
                h = height - 6;
                break;
                
                
            case  3:
                self.titleLabel = titleLabel;
                
                break;
                
            case 4:
                x = 15;
                y = height/2;
                w = WIDTH - 30;
                h = 1;
                
                self.unReadLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)-15, -3, 20, 20)];
                self.unReadLabel.backgroundColor = [UIColor redColor];
                self.unReadLabel.textColor = [UIColor whiteColor];
                self.unReadLabel.layer.cornerRadius = 10;
                self.unReadLabel.layer.masksToBounds = YES;
                self.unReadLabel.textAlignment = NSTextAlignmentCenter;
                self.unReadLabel.font = [UIFont systemFontOfSize:12];
                self.unReadLabel.text = self.UnReadCount;
                self.unReadLabel.hidden = YES;
                [menubackView addSubview:self.unReadLabel];
                
                break;
                
            case 5:
                x = 15;
                y = height;
                w = WIDTH - 15;
                h = 1;
                break;
                
            default:
                break;
        }
        
        if (i == 0 || i == 1 || i == 4 || i == 2 || i == 5) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
            view.backgroundColor = BACKGROUND_COLOR;
            [self.contentView addSubview:view];
        }
        
    }
    
}

- (void)didClickMineMenuViewList:(UITapGestureRecognizer *)sender {
    if ([self.delegate respondsToSelector:@selector(MineMenuTableViewCellDidSelectedAtIndex:)]) {
        [self.delegate MineMenuTableViewCellDidSelectedAtIndex:sender.view.tag];
    }
}

- (NSArray *)mineMenuArray {
    if (!_mineMenuArray) {
        _mineMenuArray = @[
                           @"我的可为账户",
                           @"我的代金券",
                           @"活动记录",
                           @"个人专区",
                           @"聊天列表",
                           @"我的收藏",
                           @"分享可为"
//                           @"推广系统"
                           ];
    }
    return _mineMenuArray;
}


@end
